package com.ksc.storekeeper.server.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ksc.storekeeper.server.api.UserApi;
import com.ksc.storekeeper.server.entities.Address;
import com.ksc.storekeeper.server.entities.User;
import com.ksc.storekeeper.shared.dto.UserDTO;
import com.ksc.storekeeper.shared.exceptions.ClientException;
import com.ksc.storekeeper.shared.rpc.ProfileService;
import com.ksc.storekeeper.shared.utils.conversion.ConversionUtils;

public class ProfileServiceImpl implements ProfileService {

	private final UserApi userApi;

	public ProfileServiceImpl(UserApi userApi) {
		this.userApi = userApi;
	}

	@Override
	public List<UserDTO> getAllUsers() {
		List<User> allUsers = userApi.getAllUsers();
		return (List<UserDTO>) ConversionUtils.convert(allUsers, new ArrayList<UserDTO>());
	}

	@Override
	public void addUser(UserDTO u) {
		User user = (User) ConversionUtils.convert(u, new User());
		Address add = (Address) ConversionUtils.convert(u.getAddress(), new Address());
		user.setAddress(add);
		userApi.addUser(user);
	}

	@Override
	public void changePassword(String old, String newPass) throws ClientException {
		userApi.changePassword(old, newPass);
	}

	@Override
	public void changeRole(String userName, String role) throws ClientException {
		userApi.changeRole(userName, role);

	}

}
